package com.demo.myapplication.fragment;

import android.util.Log;

import java.text.DecimalFormat;

public abstract class ReadCallBackListener {
  private String Tag = ReadCallBackListener.class.getSimpleName();

  private ThreadLocal<Long> start = new ThreadLocal<>();
  private ThreadLocal<Long> end = new ThreadLocal<>();
  private ThreadLocal<String> cost = new ThreadLocal<>();

  public void onBegin() {
    start.set(System.currentTimeMillis());
    Log.d(Tag, "thread:" + Thread.currentThread().getName() + ",start=" + start.get());
  }

  public void onFinish() {
    end.set(System.currentTimeMillis());
    Log.d(Tag, "thread:" + Thread.currentThread().getName() + ",end=" + end.get());
    //1.23s这样
    float time = (end.get() - start.get()) / 1000f;
    Log.d(Tag, "time=" + time);
    DecimalFormat df = new DecimalFormat("0.00");
//      Snackbar.make(textContent, "cost " + df.format(time) + "s", Snackbar.LENGTH_LONG).show();
    cost.set(df.format(time));
  }

  public String getTime() {
    return cost.get();
  }
}